package uiLayer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.InputMismatchException;
import java.util.Scanner;

import ctrLayer.*;

public class StockUI extends MenuUI{
	private static boolean loaded = false;
	private boolean exit;
	private Menu menu;
	
	public StockUI(){
		if(!loaded){
			FlavourCtr fCtr = new FlavourCtr();
			if(fCtr.saveExist()){
				try {
					fCtr.load();
					loaded = true;
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		try{
			menu = new Menu("Stock UI", StockUI.class);
			menu.add("Add Flavour", "addFlavour");
			menu.add("Add Bottles to Flavour", "addBottles");
			menu.add("Remove Bottles from Flavour", "removeBottles");
			menu.add("Add Sale To Flavour", "addSale");
			menu.add("List All", "printAll");
			menu.add("Graph Menu", "showGraph");
			menu.add("Toggle Flavour Limited", "makeLimited");
			menu.add("Back", "back");
			
			StockUIStart();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void StockUIStart() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		exit = false;
		while (!exit) {
			Method m = menu.run();
			if(m != null){
				m.invoke(this, null);
			}
			
		}
	}

	private void showGraph(){
		new GraphUI();
	}
	
	private void back(){
		save();
		exit = true;
	}
	
	private void save(){
		FlavourCtr fCtr = new FlavourCtr();
		try {
			fCtr.save();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void addFlavour(){
		try{
			Scanner keyboard = new Scanner(System.in);
			clearConsole();
			System.out.println("*** Add Flavour ***");
			System.out.print("Flavour Name: ");
			String fName = keyboard.nextLine();
			FlavourCtr fCtr = new FlavourCtr();
			if(fCtr.getFlavourObj(fName) == null){
				if(confirm("Do you wish to add the Flavour: " + fName)){
					fCtr.addFlavour(fName);
					save();
				}
			}else{
				System.out.println("That Flavour already exists");
				pause();
				return;
			}
		}catch(InputMismatchException e){
			System.out.println("Wrong input!");
			pause();
			return;
		}catch(NullPointerException e1){
			System.out.println(e1.getMessage());
			pause();
			return;
		}
	}
	
	private void addBottles(){
		try{
			Scanner keyboard = new Scanner(System.in);
			FlavourCtr fCtr = new FlavourCtr();
			clearConsole();
			System.out.println("*** Add Bottles ***");
			System.out.print("Flavour Name: ");
			String fName = keyboard.nextLine();
			System.out.print("Bottles(" + fCtr.getBottles(fName) + "): ");
			int bottles = keyboard.nextInt();
			keyboard.nextLine();
			if(confirm("Add " + bottles + " to " + fName)){
				fCtr.changeBottles(fName, bottles);
				save();
			}
		}catch(InputMismatchException e){
			System.out.println("Wrong input!");
			pause();
			return;
		}catch(NullPointerException e1){
			System.out.println(e1.getMessage());
			pause();
			return;
		}
	}
	
	private void removeBottles(){
		try{
			Scanner keyboard = new Scanner(System.in);
			FlavourCtr fCtr = new FlavourCtr();
			clearConsole();
			System.out.println("*** Remove Bottles ***");
			System.out.print("Flavour Name: ");
			String fName = keyboard.nextLine();
			System.out.print("Bottles(" + fCtr.getBottles(fName) + "): ");
			int bottles = keyboard.nextInt();
			keyboard.nextLine();
			if(confirm("Remove " + bottles + " from " + fName)){
				int bot = 0 - bottles;
				fCtr.changeBottles(fName, bot);
				save();
			}
		}catch(InputMismatchException e){
			System.out.println("Wrong input!");
			pause();
			return;
		}catch(NullPointerException e1){
			System.out.println(e1.getMessage());
			pause();
			return;
		}
	}
	
	private void printAll(){
		try{
			clearConsole();
			System.out.println("*** Print All ***");
			FlavourCtr fCtr = new FlavourCtr();
			System.out.println(fCtr.printAll());
			pause();
		}catch(NullPointerException e){
			System.out.println(e.getMessage());
			pause();
			return;
		}
	}
	
	private void addSale(){
		try{
			Scanner keyboard = new Scanner(System.in);
			clearConsole();
			System.out.println("*** Add Sale ***");
			System.out.print("Flavour: ");
			String flavourName = keyboard.nextLine();
			System.out.print("Amount: ");
			int amount = keyboard.nextInt();
			keyboard.nextLine();
			if(confirm("Add Sale(" + amount + ") to " + flavourName)){
				FlavourCtr fCtr = new FlavourCtr();
				fCtr.addSale(flavourName, amount);
				save();
			}
		}catch(InputMismatchException e){
			System.out.println("Wrong input");
			pause();
			return;
		}catch(NullPointerException e1){
			System.out.println(e1.getMessage());
			pause();
			return;
		}
	}
	
	private void makeLimited(){
		try{
			Scanner keyboard = new Scanner(System.in);
			clearConsole();
			System.out.println("*** Toggle Limited ***");
			System.out.print("Flavour: ");
			String flavourName = keyboard.nextLine();
			if(confirm("Toggle Limited On: " + flavourName)){
				FlavourCtr fCtr = new FlavourCtr();
				fCtr.toggleLimited(flavourName);
				save();
			}
		}catch(NullPointerException e){
			System.out.println(e.getMessage());
			pause();
			return;
		}
	}
}
